import csv
import time

import requests
from lxml import etree

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"
}


class DD():
    def __init__(self):

        self.f = open("当当网2017.csv", "w+", encoding="utf-8")

        self.f = csv.writer(self.f)
        self.f.writerow(["书名", "作者", "出版社", "出版时间", "价格", "分类"])

        # self.csvf = csv.DictWriter(self.f, ["书名", "作者", "出版社", "出版时间", "价格", "分类"])
        # self.csvf.writeheader()

    def get_fenglei(self, url):
        if not url:
            return ""
        response = requests.get(url=url, headers=headers)

        if response.status_code == 200:

            html = response.text

            html = etree.HTML(html)

            aa = html.xpath('//*[@id="detail-category-path"]/span/a/text()')

            ret = ""
            if aa:
                for i in aa:
                    ret += i + ">"
            else:
                pass

            return ret
        else:
            return None

    def save(self, items):
        print("开始写入数据")
        print(items)
        self.f.writerow(items)

        print("写入成功")

    def main(self):
        for i in range(1, 26):
            url = "http://bang.dangdang.com/books/bestsellers/01.30.00.00.00.00-year-2017-0-1-%s" % i

            response = requests.get(url=url, headers=headers)

            html = response.text

            html = etree.HTML(html)

            body = html.xpath('//ul[@class="bang_list clearfix bang_list_mode"]/li')
            # print(body)
            for li in body:
                names = li.xpath('./div[3]/a/text()')[0] if li.xpath('./div[3]/a/text()') != [] else None
                print(names)
                zuoze = li.xpath('./div[5]/a[1]/text()')[0] if li.xpath('./div[5]/a[1]/text()') != [] else None
                print(zuoze)
                chubanshe = li.xpath('./div[6]/a/text()')[0] if li.xpath('./div[6]/a/text()') != [] else None
                print(chubanshe)
                chubantime = li.xpath('./div[6]/span/text()')[0] if li.xpath('./div[6]/span/text()') != [] else None
                print(chubantime)

                comnum = li.xpath('./div[4]/a/text()')[0] if li.xpath('./div[4]/a/text()') != [] else None

                price = li.xpath('./div[7]/p[1]/span[1]/text()')[0] if li.xpath(
                    './div[7]/p[1]/span[1]/text()') != [] else None
                print(price)
                yj = li.xpath('./div[7]/p[1]/span[2]/text()')[0] if li.xpath(
                    './div[7]/p[1]/span[2]/text()') != [] else None
                zk = li.xpath('./div[7]/p[1]/span[3]/text()')[0] if li.xpath(
                    './div[7]/p[1]/span[3]/text()') != [] else None

                tjl = li.xpath('./div[4]/span[2]/text()')[0] if li.xpath('./div[4]/span[2]/text()') != [] else None
                yyy_url = li.xpath('./div[2]/a/@href')[0] if li.xpath('./div[2]/a/@href') != [] else None
                print(yyy_url)

                fl = self.get_fenglei(yyy_url)
                items = [names, zuoze, chubanshe, chubantime, price, yj, zk, comnum, tjl, fl]
                self.save(items)
            time.sleep(2)


d = DD()

d.main()
